package com.wky.org.controller;

import com.wky.basic.Result.JsonResult;
import com.wky.basic.exception.BusinessException;
import com.wky.basic.util.ExcelUtil;
import com.wky.org.pojo.Shop;
import com.wky.org.pojo.ShopAuditLog;
import com.wky.org.service.ShopService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 管理店铺
 */

@RestController
@RequestMapping("/org/shop")
@CrossOrigin
public class ShopController {
    @Autowired
    private ShopService shopService;

    /**
     * 店铺入驻
     */
    @PostMapping("/shopRegister")
    public Object shopRegister(@RequestBody Shop shop){
        try {
            shopService.shopRegister(shop);
            return JsonResult.success();
        } catch (BusinessException e) { // 业务异常
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }catch (Exception e) {  // 系统异常
            e.printStackTrace();
            return JsonResult.error("系统异常");
        }

    }

    /**
     * 店铺审核通过
     */
    @PostMapping("/shopAudit")
    public JsonResult shopAuditOk(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.shopAuditOk(shopAuditLog);
            return JsonResult.success("店铺审核通过");
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 店铺审核未通过
     */
    @PostMapping("/shopAuditNo")
    public JsonResult shopAuditNo(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.shopAuditNo(shopAuditLog);
            return JsonResult.success("店铺审核未通过");
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 店铺激活
     */
    @GetMapping("/shopActive")
    public JsonResult shopActive(@RequestParam("shopId") Long shopId){
        Shop shop = new Shop();
        shop.setId(shopId);
        shop.setState(3);
        shopService.update(shop);

        return JsonResult.success("激活成功");
    }

    /**
     * 导出Excel文件
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        // 查询所有
        List<Shop> list = shopService.findAll();
        // 导出Excel文件
        ExcelUtil.exportExcel(list, "店铺信息", "店铺信息sheet01", Shop.class, "店铺信息.xlsx", true, response);
    }

    /**
     * 导入Excel文件
     */
    @PostMapping("/import")
    public JsonResult importExcel(@RequestParam("file") MultipartFile file){
        List<Shop> list = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        //批量添加
        list.forEach(shop -> shopService.add(shop));
        return JsonResult.success("导入成功");
    }


    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public JsonResult findAll(){
        return JsonResult.success(shopService.findAll());
    }

    /**
     *  根据id查询
     */
    @GetMapping("/findById")
    public JsonResult findById(Long id){
        return JsonResult.success(shopService.findById(id));
    }

    /**
     * 分页查询
     */
    @PostMapping("/findByPage")
    public JsonResult findByPage(@RequestBody Shop shop){
        return JsonResult.success(shopService.findByPage(shop));
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody Shop shop){
        shopService.add(shop);
        return JsonResult.success();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public JsonResult update(@RequestBody Shop shop){
        shopService.update(shop);
        return JsonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById")
    public JsonResult delete(Long id){
        shopService.delete(id);
        return JsonResult.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/deleteByIds")
    public JsonResult deleteBatch(@RequestBody Long[] ids){
        for (Long id : ids) {
            shopService.delete(id);
        }
        return JsonResult.success();
    }

    /**
     * 图表统计
     */
    @GetMapping("/chart")
    public JsonResult chart(){
        return JsonResult.success(shopService.chart());
    }

}
